package cn.aku.d5_map;

import org.junit.Test;

import java.util.*;

public class Test2_HashMap {
    /*
    map遍历
    通过key
     */
    @Test
    public void test1(){
        Map<String,Integer> map=new TreeMap<>();
        map.put("java",100);
        map.put("C++",90);
        map.put("python",80);
        System.out.println(map);
        //得到Set集合
        Set<String> keys=map.keySet();
        //System.out.println(keys);
        //获取迭代器
        Iterator<String> it=keys.iterator();
        //问 取 操作
        while (it.hasNext()){
            String key=it.next();
            Integer score = map.get(key);
            System.out.println(key+" "+score);
        }
    }

    /*
     map遍历
     Entry
     */
    @Test
    public void test2(){
        Map<String,Integer> map=new TreeMap<>();
        map.put("java",100);
        map.put("C++",90);
        map.put("python",80);
        System.out.println(map);

        Set<Map.Entry<String,Integer>> entry=map.entrySet();

        Iterator<Map.Entry<String, Integer>> it = entry.iterator();
        while (it.hasNext()){
            Map.Entry<String,Integer> e=it.next();
            System.out.println(e.getKey()+","+e.getValue());
        }
    }

    /*
    通过value遍历
     */
    @Test
    public void test3(){
        Map<String,Integer> map=new TreeMap<>();
        map.put("java",100);
        map.put("C++",90);
        map.put("python",80);
        System.out.println(map);
        Collection<Integer> values = map.values();
        //value有可能重复

        //System.out.println(values.getClass().getName());
        //java.util.TreeMap$Values

        Iterator<Integer> it = values.iterator();

        while (it.hasNext()){
            Integer next = it.next();
            System.out.println(next);
        }
    }

    /*
    通过map临时存对象
     */
    @Test
    public void test4(){
        HashMap<String,Object> map=new HashMap<>();
        map.put("id",1);
        map.put("name","孙悟空");
        map.put("score",100);
        System.out.println(map);
    }
}
